import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;
import javax.imageio.*;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@SuppressWarnings("serial")
public class chessBoard extends JPanel {
	private int n;
	private static int HEIGHT,WIDTH;
	private int tileSize;
	private Color light, dark;
	private boolean odd = false;
	private boolean queens[][];
	// Defaults
	static{
		HEIGHT = WIDTH = 500;
	}
	
	public chessBoard(int n, boolean solution[][]) {
		this.n = n;	
		odd = (n%2 == 1);
		tileSize = (int)Math.ceil((double)HEIGHT/(double)n);
		HEIGHT = WIDTH = tileSize*n;
		queens = solution;
		this.setPreferredSize(new Dimension(HEIGHT,WIDTH));			
		light = new Color(250,220,180);
		dark = new Color(50,35,15);
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		int tileNum;
		
		g2.setColor(light);
		
		for(int i  = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				tileNum = (i*n)+j+1;
				
				colorSwap(g);
				if(tileNum == 1) colorSwap(g2);		// If first tile, swap back
				if((tileNum-1)%n == 0 && !odd && tileNum != 1) colorSwap(g2);
				
				g2.fillRect(j*tileSize, i*tileSize, tileSize, tileSize);
			}
		}
		
		BufferedImage img;
		try {
		    img = ImageIO.read(new File("src/queen.png"));
		    double scaleFactor = (double)tileSize/100.0;
		    g2.scale(scaleFactor,scaleFactor);
		    
		    for(int i = 0; i < n; i++){
		    	for(int j = 0; j < n; j++){
		    		if(queens[j][i] == true)
		    			g.drawImage(img, i*100, j*100, null);
		    	}
		    }
		    
		} catch (IOException e) {
		}
		
		
	}
	
	private void colorSwap(Graphics g)
	{
		if(g.getColor() == light) g.setColor(dark);
		else g.setColor(light);
	}
}
